***** RUN DO_MASTER.do ***** do "$Do_files\DO_MASTER.do"

global program "OA_FigureB36"
cap log close
cap log using "$Log_files\log_$program$today", replace

**** REPLICATION OF FIGURE B.3.6 ****

clear all
set mem 2g
set more off

// Import data

use $Data\data_news_voting, clear

// Get controls

global ctrl_base	"sh_lde lpop_A sh_F sh_F_new sh_cspsup sh_sec sh_ter lmean_reink lmean_reink2 charact_altitude charact_altitude_std sh_active sh_young sh_prot sh_M mk com_property"
global ctrl_plus	"mean_votation lcrSR_F_N"
global fe			"charact_agglo"
global fe_iv		"I_charact_agglo_*"
global cluster		"charact_agglo"

// Generate empty variables

g prior=_n-1

// To save coefficients, R2 etc later

g coef=.
g df=.
g var=.

// Regressions

forvalues foreign=0(1)100{
	ivreg2 oui (cne_F_PNP`foreign'_mk = instr_dev_mk) ///
		$ctrl_base ///
		$ctrl_plus ///
		$fe_iv ///
		if sample_hq00dstr==1, ///
		cluster($cluster) first partial($fe_iv)	
	matrix V=e(V)
	matrix b=e(b)
	replace coef=b[1, 1] if prior==`foreign'
	replace var=V[1, 1] if prior==`foreign'
	replace df=e(N)-e(df_m)-1 if prior==`foreign'
}

// Get S.E.

g se=sqrt(var)

// Keep info

keep prior coef df var se
drop if coef==.

// Get intervals

gen t=invt(df, 0.95)
gen plus=coef+t*se
gen minus=coef-t*se

// Get marker labels

g beta=coef if prior==22 | prior==46 | prior==59 | prior==100 //
tostring beta, g(b_str) force
split b_str, p(".")
g b_str3=substr(b_str2, 1, 1)
replace b_str=b_str1+"."+b_str3
replace b_str="" if b_str=="."

// Graph

sort prior

twoway (scatter beta prior, msymbol(Oh) mcolor(navy) mlabel(b_str) mlabp(n) mlabs(vsmall) sort) ///
	(line coef prior, lcolor(navy) lwidth(medium)) /// 
	(line plus prior, fcolor(navy) lcolor(navy) lpattern(dash) lwidth(medium)) ///
	(line minus prior, fcolor(navy) lcolor(navy) lpattern(dash) lwidth(medium)), ///
	legend(off)	///	
	xlabel(0 `" "0%" "(Native)""' 22 `" "22%" "(Pop Avg)" "' 46 `" "46%" "(True)" "' 59 `" "59%" "(News)" "' 100 `" "100%" "(Foreign)" "',labsize(small)) ///
	xline(0 22 46 59 100, lpattern(dot) lcolor(gs10))  /// 	
	xtitle("Prior : Share of Foreigners in Switzerland", margin(medium)) ///
	ytitle("2SLS Coefficient (90% C.I.)", margin(medium)) ///
	bgcolor(white) graphregion(color(white)) yline(0, lwidth(medthin) lpattern(dash)) ylabel(0(5)10, angle(0)format(%9.0f)) //
graph export "$Results\\$program.png", replace width(4500) height(3000)

cap log close
